import express from "express";
import { body, param } from "express-validator";
import shareController from "../controller/share.controller.js";
import tokenHandler from "../middleware/token.middleware.js";
import validateHandler from "../middleware/validate.middleware.js";

const router = express.Router();

// 创建分享链接
router.post(
  "/",
  tokenHandler,
  [
    body("fileId").isUUID().withMessage("fileId must be an UUID"),
    body("pwd").optional(),
    body("expires").optional(),
  ],
  validateHandler,
  shareController.createShare,
);

// 验证分享密码
router.post(
  "/verify/:shareId",
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID"),
    body("pwd").optional().isString().withMessage("密码必须是字符串"),
  ],
  validateHandler,
  shareController.verifyShare,
);

// 批量删除分享链接
router.delete(
  "/batch",
  tokenHandler,
  [
    body("shareIds").isArray().withMessage("shareIds must be an Array")
  ],
  validateHandler,
  shareController.deleteShares,
);

// 下载分享文件
router.get(
  "/download/:shareId",
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID"),
    body("pwd").optional().isString().withMessage("pwd must be a string"),
  ],
  validateHandler,
  shareController.downloadShare,
)

// 获取分享文件的下载链接
router.post(
  "/url/:shareId",
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID"),
    body("pwd").optional().isString().withMessage("pwd must be a string"),
  ],
  validateHandler,
  shareController.getShareUrl,
)

// 删除分享链接
router.delete(
  "/:shareId",
  tokenHandler,
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID")
  ],
  validateHandler,
  shareController.deleteShare,
);

// 获取用户的所有分享链接
router.get("/", tokenHandler, shareController.getShares);

// 获取分享链接信息
router.get(
  "/:shareId",
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID")
  ],
  validateHandler,
  shareController.getShareById,
);

//保存分享
router.post(
  "/save/:shareId",
  tokenHandler,
  [
    param("shareId").isUUID().withMessage("shareId must be an UUID"),
    body("pwd").optional().isString().withMessage("pwd must be a string"),
    param("parentId")
      .optional()
      .isUUID()
      .withMessage("parentId must be an UUID"),
  ],
  validateHandler,
  shareController.saveShare
)

export default router;
